var app = getApp();
//获得

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");
var sign = require("../../utils/sign.js");


Page({
  //页面初始化的数据
  data: {
    Username: "",
    Password: "",
    Code: "",
    time: 60,
    disabled: false,
    Citys: "",
    index: 0,
    giftShow:false,
    couponList:null
  },
  onReady: function () {
    this.Presenter.getCitys(this);
  },
  //请求数据者
  Presenter: {
    code: function (that) {

      var Data = {
        action: "code2",
        mobile: that.data.Username,
      }

      MyRequest.MyGetWithToast("Code", that, function (result) {
        //开一个定时器
        console.log(result);
        that.setData({
          disabled: true
        })

        var t = setInterval(function () {
          that.setData({ time: that.data.time - 1 })
          if (that.data.time <= 0) {
            clearInterval(t);
            that.setData({
              disabled: false,
              time: 60
            })
          }
        }, 1000);


      }, Data);
    },
    register: function (that) {
      var Data = {
        action: "register2",
        mobile: that.data.Username,
        password: that.data.Password,
        cityId: that.data.Citys[that.data.index].id,
        code: that.data.Code,
        platform: Global.getPlatform(),
        appVersion: Global.getVersion(),
      }
      var mysign = sign.getSign(Data);
      Data.sign = mysign;
      MyRequest.MyGetWithToast("Register", that, function (result) {
        if (result.data.success) {
          Global.setUserStorage(result.data.data)
          Global.setUserSign(result.data.data.userSign);
          
          var mobile = {
            mobile:that.data.Username
          }
          MyRequest.MyGet("Gift",that,function(result){
            if(result.data.success){
               //显示出大礼包列表
               that.showGift(result.data.data);
               
            }else{
               that.goMine();
            }
          },mobile)
           
         
        }
      }, Data);
    },
    getCitys: function (that) {
      var data = {
        action: "list"
      }
      MyRequest.MyGet("Citys", that, function (result) {
        that.setData({
          Citys: result.data.data
        })
        //绑定数据
      }, data)
    }
  },
  showGift:function(data){
    this.setData({giftShow:true,couponList:data})
  },
  closeGift:function(){
    this.goMine();
  },
  goMine:function(){
 wx.switchTab({
            url: '/pages/mine/mine',
            success: function (res) {
              var page = getCurrentPages().pop();
              if (page == undefined || page == null) return;
              page.onShow();
            }
          })
  },
  getCode: function () {

    this.Presenter.code(this);
  },
  doRegister: function () {
    this.Presenter.register(this);
  },
  Username_input: function (event) {
    this.setData({ Username: event.detail.value })
  },
  Password_input: function (event) {
    this.setData({ Password: event.detail.value })
  },
  Code_input: function (event) {
    this.setData({ Code: event.detail.value })
  },
  OnCityChange: function (event) {
    this.setData({
      index: event.detail.value
    })
  }
})
